package com.lc.headline.service.impl;

import com.lc.headline.dao.NewsHeadLineDao;
import com.lc.headline.dao.impl.NewsHeadLineDaoImpl;
import com.lc.headline.pojo.NewsHeadline;
import com.lc.headline.pojo.vo.HeadlineDetailVo;
import com.lc.headline.pojo.vo.HeadlinePageVo;
import com.lc.headline.pojo.vo.HeadlineQueryVo;
import com.lc.headline.service.NewsHeadlineService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liuchun
 */
public class NewsHeadlineServiceImpl implements NewsHeadlineService {

    private NewsHeadLineDao newsHeadLineDao = new NewsHeadLineDaoImpl();
    @Override
    public Map findPage(HeadlineQueryVo headlineQueryVo) {
        int pageNum = headlineQueryVo.getPageNum();
        int pageSize = headlineQueryVo.getPageSize();
        List<HeadlinePageVo> pageData = newsHeadLineDao.findPageList(headlineQueryVo);
        int totalSize = newsHeadLineDao.findPageCount(headlineQueryVo);
        int totalPage = totalSize % pageSize == 0 ? totalSize/pageSize : totalSize/pageSize +1;
        //
        Map pageInfo = new HashMap();
        pageInfo.put("pageData",pageData);
        pageInfo.put("pageNum",pageNum);
        pageInfo.put("pageSize",pageSize);
        pageInfo.put("totalPage",totalPage);
        pageInfo.put("totalSize",totalSize);

        return pageInfo;
    }

    @Override
    public HeadlineDetailVo showHeadline(int hid) {
        //头条浏览量+1
        newsHeadLineDao.addPageViews(hid);
        //查看头条详情
        return newsHeadLineDao.showHeadline(hid);
    }

    @Override
    public int addNewsHeadline(NewsHeadline newsHeadline) {
        return newsHeadLineDao.addNewsHeadline(newsHeadline);
    }

    @Override
    public boolean removeNews(Integer hid) {
        return newsHeadLineDao.removeNews(hid);
    }

    @Override
    public NewsHeadline findHeadlineByHid(int hid) {
        return newsHeadLineDao.findHeadlineByHid(hid);
    }

    @Override
    public int update(NewsHeadline newsHeadline) {
        return newsHeadLineDao.update(newsHeadline);
    }
}
